<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.oreilly.servlet.MultipartRequest" %>
<%@ page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ include file="/admin/common/inc/incSession.jsp" %>
<%

	RequestDispatcher rd = null;
	String result = "";
	String img_1 = "";
	String img_2 = "";
	String img_3 = "";
	String img_4 = "";
	File file = null;
	
	
	String realPath = ""; 			
	String savePath = "uploads/notice"; 	// 업로드 경로명.
	String saveFolder = new SimpleDateFormat("yyyyMMdd").format(new Date());	// 날짜별로 폴더 생성
	
	ServletContext context = getServletContext();
	realPath = context.getRealPath(savePath+"/"+saveFolder);
	
	
	
	File fPath = new File(realPath); //경로생성

	 if ( !fPath.exists() ) {
		 fPath.mkdirs(); //상위 디렉토리가 존재하지 않으면 상위디렉토리부터 생성.
	 }

	Enumeration files = null;
	
	int maxSize = 10 * 1024 * 1024; // 최대 업로드 파일 크기 5MB
	
	try {
		
		
		MultipartRequest multi = new MultipartRequest(request, realPath, maxSize, "UTF-8", new DefaultFileRenamePolicy());
		
		 
		img_1 = multi.getFilesystemName("img_1");
		img_2 = multi.getFilesystemName("img_2");
		img_3 = multi.getFilesystemName("img_3");
		img_4 = multi.getFilesystemName("img_4");
		 
		 
		files = multi.getFileNames();
		String name = (String)files.nextElement();         
		file = multi.getFile(name);
		 
			
		String notice_type = multi.getParameter("noticeType");
		String subject = multi.getParameter("subject");
		String content = multi.getParameter("content");
		String use_yn = multi.getParameter("useYn");
		
		param.put("notice_type",notice_type);
		param.put("subject",subject);
		param.put("content",content);
		
		if ( null != (img_1)) {
			
			param.put("img_1",savePath+"/"+saveFolder+"/"+img_1);
		 
		} 
		
		if ( null != (img_2)) {
			
			param.put("img_2",savePath+"/"+saveFolder+"/"+img_2);
		 
		} 
		
		if ( null != (img_3)) {
			
			param.put("img_3",savePath+"/"+saveFolder+"/"+img_3);
		 
		} 
		
		if ( null != (img_4)) {
			
			param.put("img_4",savePath+"/"+saveFolder+"/"+img_4);
		 
		} 
		
		param.put("use_yn",use_yn);
		
		result = sqlMap.insert("AdminNotice.insertNotice",param).toString();
		
		
		if(!"".equals(result)){
			
			out.print("<script>alert('공지사항 등록을 성공하였습니다.');</script>");
			
		} else {
			
			throw new Exception();
		}
					
	} catch (Exception e) {
			out.print("<script>alert('공지사항 등록에 실패하였습니다.');</script>");
		    e.printStackTrace();
	}
	/*
	response.sendRedirect("/admin/event/eventList.jsp?evenId="+result);
	rd = getServletContext().getRequestDispatcher("/admin/event/eventList.jsp?evenId="+result);
	rd.forward(request, response);
	*/
	if (!"".equals(result)){
%>
<script>
	parent.location.href = "noticeDetail.jsp?idx=<%=result%>";
</script>
<%		
	}
%>